
<!-- 产品管理 -->
<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="id" prop="id">
          <el-input
            v-model="queryParams.id"
            placeholder="请输入id"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="资金方" prop="escrow">
          <el-input
            v-model="queryParams.escrow"
            placeholder="请输入资金方"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
       
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
           
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
           
          >删除</el-button>
        </el-col>
        
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="productsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="产品名称" align="center" prop="productName" />
        <el-table-column label="图片" align="center" prop="image" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.image" :width="50" :height="50"/>
          </template>
        </el-table-column>
      
        <!-- <el-table-column label="资金方" align="center" prop="escrow" /> -->
        <el-table-column label="资金方" align="center" prop="escrow">
          <template slot-scope="scope">
          <span v-if="scope.row.escrow==0">小额公司</span>
          <span v-else-if="scope.row.escrow==1">大额公司</span> 
          <span v-else-if="scope.row.escrow==2">小额银行</span>
          <span v-else-if="scope.row.escrow==3">大额银行</span>  
          <span v-else-if="scope.row.escrow==4">蜗牛银行</span> 
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" prop="auditStatus" /> 
        <el-table-column align="center" sortable prop="enableStatus" label="启用状态" min-width="50">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enableStatus=='Y'?nshow:fshow" active-color="#13ce66" inactive-color="#ff4949" 
          @change="editType(scope.$index, scope.row)">
          </el-switch>
        </template>
      </el-table-column>

        <!-- <el-table-column label="启用状态" align="center" prop="enableStatus" /> -->
        <el-table-column label="月利率(%)" align="center" prop="monthRate" />
        <el-table-column label="咨询费率(%)" align="center" prop="consultingRate" />
        <el-table-column label="保证金费率(%)" align="center" prop="marginRate" />
        <el-table-column label="还款方式" align="center" prop="paymentMethod">
          <template slot-scope="scope">
          <span v-if="scope.row.paymentMethod==0">等额本息</span>
          <span v-else-if="scope.row.paymentMethod==1">等本等息</span> 
          <span v-else-if="scope.row.paymentMethod==2">先息后本</span>
          <span v-else-if="scope.row.paymentMethod==3">一次性环息付本</span>  
          </template>
        </el-table-column>
        <!-- <el-table-column label="贷款期限" align="center" prop="loanTerm" /> -->
        <el-table-column label="贷款期限" align="center" prop="loanTerm">
          <template slot-scope="scope">
          <span v-if="scope.row.loanTerm==1">1个月</span>
          <span v-else-if="scope.row.loanTerm==3">3个月</span> 
          <span v-else-if="scope.row.loanTerm==6">6个月</span>
          <span v-else-if="scope.row.loanTerm==9">9个月</span> 
          <span v-else-if="scope.row.loanTerm==12">12个月</span> 
          <span v-else-if="scope.row.loanTerm==18">18个月</span> 
          <span v-else-if="scope.row.loanTerm==24">24个月</span> 
          <span v-else-if="scope.row.loanTerm==36">36个月</span>  
          </template>
        </el-table-column>
        <el-table-column label="创建者" align="center" prop="creater" />
        <el-table-column label="创建时间" align="center" prop="creatTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.creatTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新者" align="center" prop="updater" />
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
           <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
          </template> 
        </el-table-column>
       
        <el-table-column label="操作" align="center"  width="180" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :page-sizes="[5,10,15,20]"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改产品对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="产品名称" prop="productName">
            <el-input v-model="form.productName" placeholder="请输入产品名称" />
          </el-form-item>
          <el-form-item label="图片">
            <image-upload v-model="form.image"/>
          </el-form-item>
          <el-form-item label="资金方" prop="escrow">
            <!-- <el-input v-model="form.escrow" placeholder="请输入资金方" /> -->
            <el-select v-model="form.escrow" placeholder="请选择资金方">
                  <el-option
                    v-for="dict in money"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"/>
                </el-select>
          </el-form-item>
          <!-- <el-form-item label="审核状态" prop="auditStatus">
            <el-input v-model="form.auditStatus" placeholder="请输入审核状态" />
          </el-form-item> -->
          <el-form-item label="启用状态" prop="enableStatus">
          <el-switch
            style="display: block; margin-top: 9px"
            v-model="form.enableStatus"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="激活"
            inactive-text="禁用"
            active-value="Y"
            inactive-value="N"
          >
          </el-switch>
        </el-form-item>
          <!-- <el-form-item label="启用状态" prop="enableStatus">
            <el-input v-model="form.enableStatus" placeholder="请输入启用状态" />
          </el-form-item> -->
         
          <el-form-item label="月利率(%)" prop="monthRate">
            <el-input v-model="form.monthRate" placeholder="请输入月利率" />
          </el-form-item>
          <el-form-item label="咨询费率(%)" prop="consultingRate">
            <el-input v-model="form.consultingRate" placeholder="请输入咨询费率" />
          </el-form-item>
          <el-form-item label="保证金费率(%)" prop="marginRate">
            <el-input v-model="form.marginRate" placeholder="请输入保证金费率" />
          </el-form-item>
         

          <el-form-item label="贷款期限" prop="loanTerm">
                <el-select v-model="form.loanTerm" placeholder="请选择贷款期限">
                  <el-option
                    v-for="dict in limit"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"/>
                </el-select>
          </el-form-item>
         
         
            <el-form-item label="还款方式" prop="paymentMethod">
                <el-select v-model="form.paymentMethod" placeholder="请选择还款方式">
                  <el-option
                    v-for="dict in credit"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"/>
                </el-select>
          </el-form-item>
           
          <!-- <el-form-item label="创建者" prop="creater">
            <el-input v-model="form.creater" placeholder="请输入创建者" />
          </el-form-item> -->
          <!-- <el-form-item label="创建时间" prop="creatTime">
            <el-date-picker clearable
              v-model="form.creatTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择创建时间"> 
            </el-date-picker>
          </el-form-item> -->
          <!-- <el-form-item label="修改者" prop="updater">
            <el-input v-model="form.updater" placeholder="请输入修改者" />
          </el-form-item>
          <el-form-item label="修改时间" prop="updateTime">
            <el-date-picker clearable
              v-model="form.updateTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择修改时间">
            </el-date-picker>
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import {listProducts,addProducts,updateProducts,getProducts,delProducts,searchProduct,editOneType} from "@/api/credit/creproduct.js";
  
  export default {

    name: "Product",
    data() {
      return {
        nshow: true, //switch开启
        fshow: false, //switch关闭
        //还款方式
        credit:'',
        //还款期限
        limit:'',
        //资金方
        money:'',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 产品表格数据
        productsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 5,
          id: null,
          productName: null,
          image: null,
          creater:null,
          creatTime:null,
          updater:null,
          updateTime:null,
          escrow:null,
          auditStatus:null,
          enabledStatus:null,
          monthRate:null,
          consultingRate:null,
          marginRate:null,
          paymentMethod:null,
          loanTerm:null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
    this.getList(this.queryParams);
    this.getDicts('cre_payment_method').then(res=>{
        this.credit=res.data
        console.log('zidian',res);
    })
    this.getDicts('cre_loan_term').then(res=>{
        this.limit=res.data
        console.log('zidian',res);
    })
    this.getDicts('cre_escrow').then(res=>{
        this.money=res.data
        console.log('zidian',res);
    })
  },
      watch:{
       form:function(newData,oldData){
         console.log(newData,oldData);
          this.getList();
        }
      
      },
    methods: {


      
      /** 查询产品列表 */
      getList() { 
        console.log(111);
        this.loading = true;
       
        listProducts(this.queryParams).then(response => {
          this.productsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
     
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          productName: null,
          image: null,
          creater:null,
          creatTime:null,
          updater:null,
          updateTime:null,
          escrow:null,
          enableStatus:null,
          enabledStatus:null,
          monthRate:null,
          consultingRate:null,
          marginRate:null,
          paymentMethod:null,
          loanTerm:null,
        };
        this.resetForm("form");
      },
      search(){
        searchProduct(this.queryParams).then(res=>{
          console.log(res);
          this.productsList = res.rows;
          this.total = res.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        // this.queryParams.pageNum = 1;
         this.search();

      },
      editType: function(index,row) {
      this.loading = true
      // 修改状态
      editOneType(row.id,row.enableStatus).then(res => {
        this.loading = false
        this.getList(this.queryParams)
      })
    },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        var username = this.$store.state.user.name;
        this.reset();
        this.form.creater=username;
        this.open = true;
        this.title = "添加产品";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        var username = this.$store.state.user.name;//获取账号名称
        console.log();
        this.reset();
        const id = row.id || this.ids
        getProducts(id).then(response => {
          
          console.log(111,username);
          this.form = response.data;
          this.form.updater=username;
          this.open = true;
          this.title = "修改产品";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateProducts(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addProducts(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除产品编号为"' + ids + '"的数据项？').then(function() {
          return delProducts(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/products/export', {
          ...this.queryParams
        }, `products_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  